package com.bjsxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjsxt.domain.SysMenu;
import com.bjsxt.domain.SysPrivilege;
import com.bjsxt.model.RolePrivilegesParam;
import com.bjsxt.service.SysMenuService;
import com.bjsxt.service.SysPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.mapper.SysRolePrivilegeMapper;
import com.bjsxt.domain.SysRolePrivilege;
import com.bjsxt.service.SysRolePrivilegeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements SysRolePrivilegeService{

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysPrivilegeService sysPrivilegeService;
    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;


    @Override
    /**
     * 查找对应的菜单和菜单中的权限
     */
    public List<SysMenu> findSysMenuAndPrivileges(Long roleId) {







        List<SysMenu>list = sysMenuService.list();
        if(CollectionUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        /*找到根菜单*/
        List<SysMenu> rootMenus = list.stream()
                .filter(sysMenu -> sysMenu.getParentId()==null)
                .collect(Collectors.toList());

        if(CollectionUtils.isEmpty(rootMenus)){
            return Collections.emptyList();
        }
        List<SysMenu> subMenus = new ArrayList<>();
        /* 每个根菜单的子菜单*/
        for (SysMenu rootMenu:rootMenus )
        {
            subMenus.addAll(getChildMenus(rootMenu.getId(),roleId,list));
        }


        return subMenus;
    }

    @Override
    @Transactional
    public boolean grantPrivileges(RolePrivilegesParam rolePrivilegesParam) {

        Long roleId = rolePrivilegesParam.getRoleId();
        //1.删除角色权限
       sysRolePrivilegeService.remove(new LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getId,roleId));
        //2.新增权限

            List<Long> privilegeIds = rolePrivilegesParam.getPrivilegeIds();
            if(!CollectionUtils.isEmpty(privilegeIds)){
                List<SysRolePrivilege> sysRolePrivilegess = new ArrayList<>();


                for (Long privilegeId : privilegeIds) {
                    SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                    sysRolePrivilege.setRoleId(rolePrivilegesParam.getRoleId());
                    sysRolePrivilege.setPrivilegeId(privilegeId);
                    sysRolePrivilegess.add(sysRolePrivilege);
                }
                boolean b = sysRolePrivilegeService.saveBatch(sysRolePrivilegess);
                return b;
            }

        return true;



    }

    /**
     * 查询菜单的子菜单
     * @param parentId
     * @param roleId
     * @return
     */
    private List<SysMenu> getChildMenus(Long parentId,Long roleId,List<SysMenu>sources) {

//        List<SysMenu> childs = new ArrayList<>();
//        for (SysMenu source : sources) {
//            if (source.getParentId() == parentId) { // 找儿子
//                childs.add(source);
//                source.setChilds(getChildMenus(source.getId(), roleId, sources)); // 给该儿子设置儿子
//                List<SysPrivilege> sysPrivileges = sysPrivilegeService.getAllSysPrivilege(source.getId(), roleId);
//                source.setSysPrivileges(sysPrivileges); // 该儿子可能包含权限
//            }
//        }
//        return childs;








        ArrayList<SysMenu> childs = new ArrayList<>();
        for (SysMenu source : sources) {
            if( source.getParentId() == parentId)
            {
                childs.add(source);
                source.setChilds(getChildMenus(source.getId(),roleId,sources));

                List<SysPrivilege>list = sysPrivilegeService.getAllSysPrivilege(source.getId(),roleId);
                source.setSysPrivileges(list);
            }
        }

        return childs;
    }

}
